package com.wuhan.template.module.home.activity;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.text.Editable;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.wuhan.template.R;
import com.wuhan.template.base.BaseActivity;
import com.wuhan.template.constant.Urls;
import com.wuhan.template.module.home.adapter.SearchRecyclerAdapter;
import com.wuhan.template.module.home.entity.HotSearchEntity;
import com.wuhan.template.module.home.other.SpacesItemDecoration;
import com.wuhan.template.utils.EventMessage;
import com.wuhan.template.utils.OkHttpUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Administrator on 2016/5/13 0013.
 */
public class HomeSearchActivity extends BaseActivity{

    @BindView(R.id.search_recyclerView)
    protected RecyclerView recyclerView ;

    @BindView(R.id.search_editText)
    protected EditText editText;

    @BindView(R.id.searchImage)
    protected ImageView searchImage;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_home_search;
    }

    @Override
    protected void initViews() {
        final Editable text = editText.getText();
        searchImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(HomeSearchActivity.this,SearchListActivity.class);
                intent.putExtra("keyWord",text);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void loadData() {
        OkHttpUtils.get(Urls.HOME_SEARCH,null);
    }

    @Override
    protected void postEventResult(@NonNull EventMessage message) {

        if(message.type.equals(Urls.HOME_SEARCH)){
            Gson gson = new Gson();
            HotSearchEntity[] hotSearchEntities = gson.fromJson(message.result, HotSearchEntity[].class);
            List<HotSearchEntity> hotSearchLists  = new ArrayList<>();
            hotSearchLists.addAll(Arrays.asList(hotSearchEntities));
            SearchRecyclerAdapter adapter = new SearchRecyclerAdapter(this,hotSearchLists);
            recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
            recyclerView.setAdapter(adapter);
            //设定item的间隔
            SpacesItemDecoration decoration=new SpacesItemDecoration(16);
            recyclerView.addItemDecoration(decoration);

            adapter.setOnItemClickListener(new SearchRecyclerAdapter.OnRecyclerViewItemClickListener() {
                @Override
                public void onItemClick(View view, String data) {
                    Toast.makeText(HomeSearchActivity.this, "点击了" + data, Toast.LENGTH_SHORT).show();
                    editText.setText(data);

                    Intent intent = new Intent(HomeSearchActivity.this,SearchListActivity.class);
                    intent.putExtra("keyWord",data);
                    startActivity(intent);
                }
            });

        }

    }


}
